package com.mytech.cslive.infrastructure;

import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;

public class AppConfig {
	public static final String PREF_FILE_NAME = "AppConfig";
	private static Context mainContext;
	
	public static Context getMainContext() {
		return mainContext;
	}

	public static void setMainContext(Context mainContext) {
		AppConfig.mainContext = mainContext;
	}
	
	public static SharedPreferences getDefaultSharedPreferences() {
		return (PreferenceManager.getDefaultSharedPreferences(getMainContext()));
	}
	
	public static void setProperty(String key, String value) {
		if (key != null && key != "" && value != null) {
			SharedPreferences sharedPreferences = getDefaultSharedPreferences();
			
			SharedPreferences.Editor editor = sharedPreferences.edit();
			editor.putString(key, value);
			editor.commit();
		}
	}
	
	public static void setProperty(String key, int value) {
		setProperty(key, Integer.toString(value));
	}
	
	public static void setProperty(String key, double value) {
		setProperty(key, Double.toString(value));
	}
	
	public static void setNonEmptyProperty(String key, String value) {
		if (!value.equals("")) {
			setProperty(key, value);
		}
	}
	
	/**
	 * Returns property or null if it doesn't exist
	 * @param key
	 * @return
	 */
	public static String getProperty(String key) {
		return (getDefaultSharedPreferences().getString(key, null));
	}
} 
